package com.dynamo.transport.network.practise.channel.socket.nio;

import com.dynamo.transport.network.practise.channel.*;
import com.dynamo.transport.network.practise.channel.socket.SocketChannel;

import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;

/**
 * @author duanyimiao
 * @create 2018-12-17 10:33 AM
 * @description
 **/
public class NioServerSocketChannel extends AbstractChannel implements SocketChannel {
    //属性允许包内访问 TODO 应该依赖于抽象workerPool
    NioBossPool bossPool;
    NioWorkerPool workerPool;
    ServerSocketChannel channel;
    private SocketAddress localAddress;
    private ChannelPipeline pipeline;
    private ChannelFuture future;

    public NioServerSocketChannel() {

    }

    public NioServerSocketChannel(SocketAddress localAddress, ChannelPipeline pipeline, NioBossPool bossPool,NioWorkerPool workerPool) {

        this.localAddress = localAddress;
        this.bossPool = bossPool;
        this.workerPool = workerPool;
        this.pipeline = pipeline;
        openChannel();
    }

    private void openChannel() {
        try {
            this.channel = ServerSocketChannel.open();
            this.channel.configureBlocking(false);
            //创建一个Server Channel
            Channels.fireChannelOpen(pipeline, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ChannelConfig getConfig() {
        return null;
    }

    @Override
    public ChannelPipeline getPipeline() {
        return pipeline;
    }

    @Override
    public ChannelFuture write(Object msg) {
        return null;
    }

    @Override
    public ChannelFuture bind(SocketAddress socketAddress) {
        return null;
    }

    @Override
    public SocketAddress getLocalAddress() {
        return localAddress;
    }

    @Override
    public SocketAddress getRemoteAddress() {
        return null;
    }
}
